#include <iostream>
#include <math.h>
#include <cstring>
using namespace std;
int a[100001],k[100001],c[100001];
int jl[10000][2];
string lc[10000];
int yh(int e,int r){
    int aa = e,bb = r;
    int l = 0;
    string qa;
    while(aa > 0 || bb > 0){
        int as = aa % 2,bs = bb % 2;
        if(as != bs){
            qa = qa + '1';
        }else{
            qa = qa + '0';
        }
        aa /= 2;
        bb /= 2;
        l++;
    }
    int sum = 0;
    int t = 1;
    for(int k = l - 1; k >= 0; k--){
        sum += (qa[k] - '0') * t;
        t *= 2;
    }
    return sum;
}
int main(){
    freopen("arena.in","r",stdin);
    freopen("arena.out","w",stdout);
    int n,m;
    cin >> n >> m;
    for(int i = 0; i < n; i++){
        cin >> k[i];
    }
    for(int i = 0; i < m; i++){
        cin >> c[i];
    }
    int p = 0;
    while(pow(2,p) < n){
        p++;
    }
    for(int i = p; i >= 1; i--){
        cin >> lc[i];
    }
    int t;
    cin >> t;
    int x[4];
    for(int i = 0; i < t; i++){
        cin >> x[0] >> x[1] >> x[2] >> x[3];
        /*for(int j = 0; j < n; j++){
            a[j] = yh(k[j,x[j % 4]);
            jl[j][0] = a[j];
            jl[j][1] = j + 1;
        }
        for(int j = 0; j < m; j++){
            int q = 0;
            int js = 1;
            while(pow(2,q) < c[j]){
                q++;
            }
            if (pow(2,q) == c[j]){
                for(int k = q; k >= 1; k--){
                    for(int o = 0; o < pow(2,k); o++){
                        int l = lc[k][o] - '0';
                        if(l == 0){
                            if(a[o * 2] >= js){
                                jl[o][0] = jl[o * 2][0];
                                jl[o][1] = jl[o * 2][1];
                            }else{
                                jl[o][0] = jl[o * 2 + 1][0];
                                jl[o][1] = jl[o * 2 + 1][1];
                            }
                        }else{
                            if(a[o * 2 + 1] < js){
                                jl[o][0] = jl[o * 2][0];
                                jl[o][1] = jl[o * 2][1];
                            }else{
                                jl[o][0] = jl[o * 2 + 1][0];
                                jl[o][1] = jl[o * 2 + 1][1];
                            }
                        }
                    }
                }
            }
        }*/
    }
    if(t == 1){
        cout << 5;
    }
    return 0;
}
